﻿using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using LinqBll;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace TestLinqBll
{
    [TestClass]
    public class TestDataContextCore
    {
        private ICore<Product> baseCore = DbFactory.Create<Product>();
        [TestMethod]
        public void TestCreate()
        {
            baseCore.Create(new Product()
            {
                adddate = DateTime.Now,
                name = "abdc",
                Status = 1
            });
        }

        [TestMethod]
        public void TestUpdate()
        {
            //这种就是同一个DataContext对象
            var t = baseCore.QueryList().FirstOrDefault();
            if (t != null)
            {
                t.name = "update";
                baseCore.Update(t);
            }
            Assert.AreEqual(1, baseCore.QueryList(s => s.name == "update").Count());
        }

        [TestMethod]
        public void TestUpdate2()
        {
            //这种方式下因为你只设置了两个数据所以会把其他没有数据的字段编程NULL
            baseCore.Update(new Product()
            {
                Id = 4,
                name = "update2"
            });
            Assert.AreEqual(1, baseCore.QueryList(s => s.name == "update2").Count());
        }

        [TestMethod]
        public void TestQuery()
        {
            var t = baseCore.QueryList().FirstOrDefault();
            baseCore.Delete(t);
        }

        [TestMethod]
        public void TestDelete()
        {
            var t = baseCore.Query(s => s.Id != 0);
            if (t != null)
                baseCore.Delete(t);
        }

        [TestMethod]
        public void TestCreateList()
        {
            baseCore.CreateList(new List<Product>
            {new Product()
            {
                adddate = DateTime.Now,
                name = "createlist",
                Status = 1 
            },new Product()
            {
                adddate = DateTime.Now,
                name = "createlist",
                Status = 1
            },new Product()
            {
                adddate = DateTime.Now,
                name = "createlist",
                Status = 1
            },new Product()
            {
                adddate = DateTime.Now,
                name = "createlist",
                Status = 1
            },new Product()
            {
                adddate = DateTime.Now,
                name = "createlist",
                Status = 1
            }});

            Assert.AreEqual(5, baseCore.QueryList(s => s.name == "createlist").Count());
        }

        [TestMethod]
        public void TestUpdateList()
        {
            var ts = baseCore.QueryList().ToList();
            foreach (var i in ts)
            {
                i.name = "updatelist";
            }
            baseCore.Update(ts);
            Assert.AreEqual(ts.Count(), baseCore.QueryList(s => s.name == "updatelist").Count());
        }

        [TestMethod]
        public void TestDeleteList()
        {
            var ts = baseCore.QueryList();
            baseCore.Delete(ts);
            Assert.AreEqual(0, baseCore.QueryList().Count());
        }
    }
}
